[bits 32]

global _memset
global _memcpy
global _memmove
global _strlen
global _strcpy
global _strcat
global _strcmp

section .text

;function memcpy
;void memcpy(void *dest,void *src,unsigned long length);
;
_memcpy:
	push	ebp
	mov	ebp,esp
	push	esi
	push	edi
	push	eax
	push	ecx
	push	es

	push	ds
	pop	es
	mov	edi,dword [ebp+8]
	mov	esi,dword [ebp+12]
	mov	eax,dword [ebp+16]
	mov	ecx,eax
	shr	ecx,2			; ecx = length / 4
	rep	movsd
	test	eax,2
	je	short read_byte
	movsw
read_byte:
	test	eax,1
	je	short end_memcpy
	movsb
end_memcpy:
	pop	es
	pop	ecx
	pop	eax
	pop	edi
	pop	esi
	pop	ebp
	ret

;function strlen
;unsigned int strlen(char *str);
;
_strlen:
	mov     ecx,[esp + 4]			; ecx -> string
	test    ecx,3					; test if string is aligned on 32 bits
	je      short main_loop

str_misaligned:
	; simple byte loop until string is aligned
	mov     al,byte [ecx]
	add     ecx,1
	test    al,al
	je      short byte_3
	test    ecx,3
	jne     short str_misaligned

	add     eax,0     			    ; 5 byte nop to align label below
	align   16                      ; should be redundant

main_loop:
	mov     eax,dword [ecx]     	; read 4 bytes
	mov     edx,0x7efefeff
	add     edx,eax
	xor     eax,0xffffffff
	xor     eax,edx
	add     ecx,4
	test    eax,81010100h
	je      short main_loop
	
	; found zero byte in the loop
	mov     eax,[ecx - 4]
	test    al,al                   ; is it byte 0
	je      short byte_0
	test    ah,ah                   ; is it byte 1
	je      short byte_1
	test    eax,00ff0000h           ; is it byte 2
	je      short byte_2
	test    eax,0ff000000h          ; is it byte 3
	je      short byte_3
	jmp     short main_loop         ; taken if bits 24-30 are clear and bit
									; 31 is set

byte_3:
	lea     eax,[ecx - 1]
	mov     ecx,[esp + 4]			; [esp + 4] = string
	sub     eax,ecx
	ret

byte_2:
	lea     eax,[ecx - 2]
	mov     ecx,[esp + 4]			; [esp + 4] = string
	sub     eax,ecx
	ret

byte_1:
	lea     eax,[ecx - 3]
	mov     ecx,[esp + 4]			; [esp + 4] = string
	sub     eax,ecx
	ret
	
byte_0:
	lea     eax,[ecx - 4]
	mov     ecx,[esp + 4]			; [esp + 4] = string
	sub     eax,ecx
	ret


; function memset
; void* memset(void* dst, int val, size_t sz)
_memset:
	push	ebp
	mov	ebp,esp
	push	edi
	mov	edi,[ebp+8]
	mov	eax,[ebp+12]
	mov	ecx,[ebp+16]
	cld

	; We will handle memsets of <= 15 bytes one byte at a time.
	; This avoids some extra overhead for small memsets, and
	; knowing we are setting > 15 bytes eliminates some annoying
	; checks in the "long move" case.
	
	cmp	ecx,15
	jle	short L3

	; Otherwise, tile the byte value out into %eax.
	; 0x41 -> 0x41414141, etc.

	mov	ah,al
	mov	edx,eax
	sal	eax,16
	mov	ax,dx
	jmp	short L2

	; Handle any cruft necessary to get %edi long-aligned.

L1:	stosb
	dec	ecx
L2:	test	edi,3
	jnz	short L1

	; Now slam out all of the longs.

	mov	edx,ecx
	shr	ecx,2
	rep	stosd

	; Finally, handle any trailing cruft.  We know the high three bytes
	; of %ecx must be zero, so just put the "slop count" in the low byte.

	mov	cl,dl
	and	cl,3

L3:	rep	stosb
	pop	edi
	mov	eax,[ebp+8]
	leave
	ret

; function strcmp
; int strcmp(const char *str1, const char *str2)
_strcmp:
	mov     edx,[esp + 4]   ; edx = src
	mov     ecx,[esp + 8]   ; ecx = dst

	test    edx,3
	jnz     short dopartial

	align   4
dodwords:
	mov     eax,[edx]
	
	cmp     al,[ecx]
	jne     short donene
	or      al,al
	jz      short doneeq
	cmp     ah,[ecx + 1]
	jne     short donene
	or      ah,ah
	jz      short doneeq
	
	shr     eax,16

	cmp     al,[ecx + 2]
	jne     short donene
	or      al,al
	jz      short doneeq
	cmp     ah,[ecx + 3]
	jne     short donene
	add     ecx,4
	add     edx,4
	or      ah,ah
	jnz     short dodwords

	align   4
doneeq:
	xor     eax,eax
	ret

	align   4
donene:
	; The instructions below should place -1 in eax if src < dst,
	; and 1 in eax if src > dst.

	sbb     eax,eax
	sal     eax,1
	add     eax,1
	ret

	align   4
dopartial:
	test    edx,1
	jz      short doword

	mov     al,[edx]
	add     edx,1
	cmp     al,[ecx]
	jne     short donene
	add     ecx,1
	or      al,al
	jz      short doneeq

	test    edx,2
	jz      short dodwords

	align   4
doword:
	mov     ax,[edx]
	add     edx,2
	cmp     al,[ecx]
	jne     short donene
	or      al,al
	jz      short doneeq
	cmp     ah,[ecx + 1]
	jne     short donene
	or      ah,ah
	jz      short doneeq
	add     ecx,2
	jmp     short dodwords

; end of _strcmp

_strcpy:

        push    edi                 ; preserve edi
        mov     edi,[esp+8]         ; edi points to dest string
        jmp     short _strcpy_copy_start

        align   16

_strcat:

        mov     ecx,[esp+4]         ; ecx -> dest string
        push    edi                 ; preserve edi
        test    ecx,3               ; test if string is aligned on 32 bits
        je      short _strcpy_find_end_of_dest_string_loop

_strcpy_dest_misaligned:                    ; simple byte loop until string is aligned
        mov     al,[ecx]
        add     ecx,1
        test    al,al
        je      short _strcpy_start_byte_3
        test    ecx,3
        jne     short _strcpy_dest_misaligned

        align   4

_strcpy_find_end_of_dest_string_loop:
        mov     eax,[ecx] ; read 4 bytes
        mov     edx,0x7efefeff
        add     edx,eax
        xor     eax,-1
        xor     eax,edx
        add     ecx,4
        test    eax,0x81010100
        je      short _strcpy_find_end_of_dest_string_loop
        ; found zero byte in the loop
        mov     eax,[ecx - 4]
        test    al,al               ; is it byte 0
        je      short _strcpy_start_byte_0
        test    ah,ah               ; is it byte 1
        je      short _strcpy_start_byte_1
        test    eax,0x00ff0000       ; is it byte 2
        je      short _strcpy_start_byte_2
        test    eax,0x0ff000000      ; is it byte 3
        je      short _strcpy_start_byte_3
        jmp     short _strcpy_find_end_of_dest_string_loop
                                    ; taken if bits 24-30 are clear and bit
                                    ; 31 is set
_strcpy_start_byte_3:
        lea     edi,[ecx - 1]
        jmp     short _strcpy_copy_start
_strcpy_start_byte_2:
        lea     edi,[ecx - 2]
        jmp     short _strcpy_copy_start
_strcpy_start_byte_1:
        lea     edi,[ecx - 3]
        jmp     short _strcpy_copy_start
_strcpy_start_byte_0:
        lea     edi,[ecx - 4]
;       jmp     short _strcpy_copy_start

;       edi points to the end of dest string.
_strcpy_copy_start:
        mov     ecx,[esp+0ch]       ; ecx -> sorc string
        test    ecx,3               ; test if string is aligned on 32 bits
        je      short _strcpy_main_loop_entrance

_strcpy_src_misaligned:                     ; simple byte loop until string is aligned
        mov     dl,[ecx]
        add     ecx,1
        test    dl,dl
        je      short _strcpy_byte_0
        mov     [edi],dl
        add     edi,1
        test    ecx,3
        jne     short _strcpy_src_misaligned
        jmp     short _strcpy_main_loop_entrance

_strcpy_main_loop:                          ; edx contains first dword of sorc string
        mov     [edi],edx           ; store one more dword
        add     edi,4               ; kick dest pointer
_strcpy_main_loop_entrance:
        mov     edx,0x7efefeff
        mov     eax,[ecx] ; read 4 bytes

        add     edx,eax
        xor     eax,-1

        xor     eax,edx
        mov     edx,[ecx]           ; it's in cache now

        add     ecx,4               ; kick dest pointer
        test    eax,0x81010100

        je      short _strcpy_main_loop
        ; found zero byte in the loop

        test    dl,dl               ; is it byte 0
        je      short _strcpy_byte_0
        test    dh,dh               ; is it byte 1
        je      short _strcpy_byte_1
        test    edx,0x00ff0000       ; is it byte 2
        je      short _strcpy_byte_2
        test    edx,0x0ff000000      ; is it byte 3
        je      short _strcpy_byte_3
        jmp     short _strcpy_main_loop     ; taken if bits 24-30 are clear and bit
                                    ; 31 is set
_strcpy_byte_3:
        mov     [edi],edx
        mov     eax,[esp+8]         ; return in eax pointer to dest string
        pop     edi
        ret
_strcpy_byte_2:
        mov     [edi],dx
        mov     eax,[esp+8]         ; return in eax pointer to dest string
        mov     byte [edi+2],0
        pop     edi
        ret
_strcpy_byte_1:
        mov     [edi],dx
        mov     eax,[esp+8]         ; return in eax pointer to dest string
        pop     edi
        ret
_strcpy_byte_0:
        mov     [edi],dl
        mov     eax,[esp+8]         ; return in eax pointer to dest string
        pop     edi
        ret

; end of strcat
; end of strcpy

;memmove - Copy source buffer to destination buffer
;
;Purpose:
;       memmove() copies a source memory buffer to a destination memory buffer.
;       This routine recognize overlapping buffers to avoid propogation.
;       For cases where propogation is not a problem, memcpy() can be used.
;
;   Algorithm:
;
;       void * memmove(void * dst, void * src, size_t count)
;       {
;               void * ret = dst;
;
;               if (dst <= src || dst >= (src + count)) {
;                       /*
;                        * Non-Overlapping Buffers
;                        * copy from lower addresses to higher addresses
;                        */
;                       while (count--)
;                               *dst++ = *src++;
;                       }
;               else {
;                       /*
;                        * Overlapping Buffers
;                        * copy from higher addresses to lower addresses
;                        */
;                       dst += count - 1;
;                       src += count - 1;
;
;                       while (count--)
;                               *dst-- = *src--;
;                       }
;
;               return(ret);
;       }
;
;
;Entry:
;       void *dst = pointer to destination buffer
;       const void *src = pointer to source buffer
;       size_t count = number of bytes to copy
;
;Exit:
;       Returns a pointer to the destination buffer in AX/DX:AX
;
;Uses:
;       CX, DX
;
;Exceptions:
;*******************************************************************************

_memmove:

        push    ebp             ;U - save old frame pointer
        mov     ebp, esp        ;V - set new frame pointer

        push    edi             ;U - save edi
        push    esi             ;V - save esi

        mov     esi,[ebp + 12]       ;U - esi = source	(ebp+0c)
        mov     ecx,[ebp + 16]     ;V - ecx = number of bytes to move (ebp+10)

        mov     edi,[ebp + 8]       ;U - edi = dest (ebp+8)

;
; Check for overlapping buffers:
;       If (dst <= src) Or (dst >= src + Count) Then
;               Do normal (Upwards) Copy
;       Else
;               Do Downwards Copy to avoid propagation
;

        mov     eax,ecx         ;V - eax = byte count...

        mov     edx,ecx         ;U - edx = byte count...
        add     eax,esi         ;V - eax = point past source end

        cmp     edi,esi         ;U - dst <= src ?
        jbe     short _memmove_CopyUp    ;V - yes, copy toward higher addresses

        cmp     edi,eax         ;U - dst < (src + count) ?
        jb      _memmove_CopyDown        ;V - yes, copy toward lower addresses

;
; Copy toward higher addresses.
;
_memmove_CopyUp:
;
; First, see if we can use a "fast" copy SSE2 routine
        ; block size greater than min threshold?
        cmp     ecx,0x100
        jb      _memmove_Dword_align

;
; The algorithm for forward moves is to align the destination to a dword
; boundary and so we can move dwords with an aligned destination.  This
; occurs in 3 steps.
;
;   - move x = ((4 - Dest & 3) & 3) bytes
;   - move y = ((L-x) >> 2) dwords
;   - move (L - x - y*4) bytes
;

_memmove_Dword_align:
        test    edi, 3         ;U - destination dword aligned?
        jnz     short _memmove_CopyLeadUp ;V - if we are not dword aligned already, align

        shr     ecx,2           ;U - shift down to dword count
        and     edx,3         ;V - trailing byte count

        cmp     ecx,8           ;U - test if small enough for unwind copy
        jb      short _memmove_CopyUnwindUp ;V - if so, then jump

        rep     movsd           ;N - move all of our dwords

        jmp     dword [_memmove_TrailUpVec + edx*4] ;N - process trailing bytes

;
; Code to do optimal memory copies for non-dword-aligned destinations.
;

; The following length check is done for two reasons:
;
;    1. to ensure that the actual move length is greater than any possiale
;       alignment move, and
;
;    2. to skip the multiple move logic for small moves where it would
;       be faster to move the bytes with one instruction.
;

        align   2
_memmove_CopyLeadUp:

        mov     eax,edi         ;U - get destination offset
        mov     edx,3         ;V - prepare for mask

        sub     ecx,4           ;U - check for really short string - sub for adjust
        jb      short _memmove_ByteCopyUp ;V - branch to just copy bytes

        and     eax,3         ;U - get offset within first dword
        add     ecx,eax         ;V - update size after leading bytes copied

        jmp     dword [_memmove_LeadUpVec + eax*4-4] ;N - process leading bytes

        align   2
        
_memmove_ByteCopyUp:
        jmp     dword [_memmove_TrailUpVec + ecx*4+16] ;N - process just bytes

        align   2
        
_memmove_CopyUnwindUp:
        jmp     dword [_memmove_UnwindUpVec + ecx*4] ;N - unwind dword copy

        align   2
        
_memmove_LeadUp1:
        and     edx,ecx         ;U - trailing byte count
        mov     al,[esi]        ;V - get first byte from source

        mov     [edi],al        ;U - write second byte to destination
        mov     al,[esi+1]      ;V - get second byte from source

        mov     [edi+1],al      ;U - write second byte to destination
        mov     al,[esi+2]      ;V - get third byte from source

        shr     ecx,2           ;U - shift down to dword count
        mov     [edi+2],al      ;V - write third byte to destination

        add     esi,3           ;U - advance source pointer
        add     edi,3           ;V - advance destination pointer

        cmp     ecx,8           ;U - test if small enough for unwind copy
        jb      short _memmove_CopyUnwindUp ;V - if so, then jump

        rep     movsd           ;N - move all of our dwords

        jmp     dword [_memmove_TrailUpVec + edx*4] ;N - process trailing bytes

        align   2
        
_memmove_LeadUp2:
        and     edx,ecx         ;U - trailing byte count
        mov     al,[esi]        ;V - get first byte from source

        mov     [edi],al        ;U - write second byte to destination
        mov     al,[esi+1]      ;V - get second byte from source

        shr     ecx,2           ;U - shift down to dword count
        mov     [edi+1],al      ;V - write second byte to destination

        add     esi,2           ;U - advance source pointer
        add     edi,2           ;V - advance destination pointer

        cmp     ecx,8           ;U - test if small enough for unwind copy
        jb      short _memmove_CopyUnwindUp ;V - if so, then jump

        rep     movsd           ;N - move all of our dwords

        jmp     dword [_memmove_TrailUpVec + edx*4] ;N - process trailing bytes

        align   2

_memmove_LeadUp3:
        and     edx,ecx         ;U - trailing byte count
        mov     al,[esi]        ;V - get first byte from source

        mov     [edi],al        ;U - write second byte to destination
        add     esi,1           ;V - advance source pointer

        shr     ecx,2           ;U - shift down to dword count
        add     edi,1           ;V - advance destination pointer

        cmp     ecx,8           ;U - test if small enough for unwind copy
        jb      short _memmove_CopyUnwindUp ;V - if so, then jump

        rep     movsd           ;N - move all of our dwords

        jmp     dword [_memmove_TrailUpVec + edx*4] ;N - process trailing bytes

        align   2

_memmove_LeadUpVec:
		dd	_memmove_LeadUp1
		dd	_memmove_LeadUp2
		dd	_memmove_LeadUp3
		
		align	2

_memmove_UnwindUp7:
        mov     eax,[esi+ecx*4-28] ;U - get dword from source
                                   ;V - spare
        mov     [edi+ecx*4-28],eax ;U - put dword into destination
_memmove_UnwindUp6:
        mov     eax,[esi+ecx*4-24] ;U(entry)/V(not) - get dword from source
                                   ;V(entry) - spare
        mov     [edi+ecx*4-24],eax ;U - put dword into destination
_memmove_UnwindUp5:
        mov     eax,[esi+ecx*4-20] ;U(entry)/V(not) - get dword from source
                                   ;V(entry) - spare
        mov     [edi+ecx*4-20],eax ;U - put dword into destination
_memmove_UnwindUp4:
        mov     eax,[esi+ecx*4-16] ;U(entry)/V(not) - get dword from source
                                   ;V(entry) - spare
        mov     [edi+ecx*4-16],eax ;U - put dword into destination
_memmove_UnwindUp3:
        mov     eax,[esi+ecx*4-12] ;U(entry)/V(not) - get dword from source
                                   ;V(entry) - spare
        mov     [edi+ecx*4-12],eax ;U - put dword into destination
_memmove_UnwindUp2:
        mov     eax,[esi+ecx*4-8] ;U(entry)/V(not) - get dword from source
                                  ;V(entry) - spare
        mov     [edi+ecx*4-8],eax ;U - put dword into destination
_memmove_UnwindUp1:
        mov     eax,[esi+ecx*4-4] ;U(entry)/V(not) - get dword from source
                                  ;V(entry) - spare
        mov     [edi+ecx*4-4],eax ;U - put dword into destination

        lea     eax,[ecx*4]     ;V - compute update for pointer

        add     esi,eax         ;U - update source pointer
        add     edi,eax         ;V - update destination pointer
_memmove_UnwindUp0:
        jmp     dword [_memmove_TrailUpVec + edx*4] ;N - process trailing bytes

		align	2
		
_memmove_UnwindUpVec:
		dd	_memmove_UnwindUp0
		dd	_memmove_UnwindUp1
		dd	_memmove_UnwindUp2
		dd	_memmove_UnwindUp3
		dd	_memmove_UnwindUp4
		dd	_memmove_UnwindUp5
		dd	_memmove_UnwindUp6
		dd	_memmove_UnwindUp7

;-----------------------------------------------------------------------------

        align   2

_memmove_TrailUp0:
        mov     eax,[ebp + 8]       ;U - return pointer to destination
        pop     esi             ;V - restore esi
        pop     edi             ;U - restore edi
                                ;V - spare
        leave
        ret

        align   2
        
_memmove_TrailUp1:
        mov     al,[esi]        ;U - get byte from source
                                ;V - spare
        mov     [edi],al        ;U - put byte in destination
        mov     eax,[ebp + 8]       ;V - return pointer to destination
        pop     esi             ;U - restore esi
        pop     edi             ;V - restore edi
        leave
        ret

        align   2
_memmove_TrailUp2:
        mov     al,[esi]        ;U - get first byte from source
                                ;V - spare
        mov     [edi],al        ;U - put first byte into destination
        mov     al,[esi+1]      ;V - get second byte from source
        mov     [edi+1],al      ;U - put second byte into destination
        mov     eax,[ebp + 8]       ;V - return pointer to destination
        pop     esi             ;U - restore esi
        pop     edi             ;V - restore edi
        leave
        ret

        align   2
_memmove_TrailUp3:
        mov     al,[esi]        ;U - get first byte from source
                                ;V - spare
        mov     [edi],al        ;U - put first byte into destination
        mov     al,[esi+1]      ;V - get second byte from source
        mov     [edi+1],al      ;U - put second byte into destination
        mov     al,[esi+2]      ;V - get third byte from source
        mov     [edi+2],al      ;U - put third byte into destination
        mov     eax,[ebp + 8]       ;V - return pointer to destination
        pop     esi             ;U - restore esi
        pop     edi             ;V - restore edi
        leave
        ret

		align	2
		
_memmove_TrailUpVec:
		dd	_memmove_TrailUp0
		dd	_memmove_TrailUp1
		dd	_memmove_TrailUp2
		dd	_memmove_TrailUp3

;-----------------------------------------------------------------------------
;
; Copy down to avoid propogation in overlapping buffers.
;
        align   2

_memmove_CopyDown:
        lea     esi,[esi+ecx-4] ;U - point to 4 bytes before src buffer end
        lea     edi,[edi+ecx-4] ;V - point to 4 bytes before dest buffer end
;
; See if the destination start is dword aligned
;

        test    edi,3         ;U - test if dword aligned
        jnz     short _memmove_CopyLeadDown ;V - if not, jump

        shr     ecx,2           ;U - shift down to dword count
        and     edx,3         ;V - trailing byte count

        cmp     ecx,8           ;U - test if small enough for unwind copy
        jb      short _memmove_CopyUnwindDown ;V - if so, then jump

        std                     ;N - set direction flag
        rep     movsd           ;N - move all of our dwords
        cld                     ;N - clear direction flag back

        jmp     dword [_memmove_TrailDownVec + edx*4] ;N - process trailing bytes

        align   2

_memmove_CopyUnwindDown:
        neg     ecx             ;U - negate dword count for table merging
                                ;V - spare

        jmp     dword [_memmove_UnwindDownVec + ecx*4+28] ;N - unwind copy

        align   2
        
_memmove_CopyLeadDown:

        mov     eax,edi         ;U - get destination offset
        mov     edx,3         ;V - prepare for mask

        cmp     ecx,4           ;U - check for really short string
        jb      short _memmove_ByteCopyDown ;V - branch to just copy bytes

        and     eax,3         ;U - get offset within first dword
        sub     ecx,eax         ;U - to update size after lead copied

        jmp     dword [_memmove_LeadDownVec + eax*4-4] ;N - process leading bytes

        align   2

_memmove_ByteCopyDown:
        jmp     dword [_memmove_TrailDownVec + ecx*4] ;N - process just bytes

        align   2
        
_memmove_LeadDownVec:
		dd	_memmove_LeadDown1
		dd	_memmove_LeadDown2
		dd	_memmove_LeadDown3

        align   2

_memmove_LeadDown1:
        mov     al,[esi+3]      ;U - load first byte
        and     edx,ecx         ;V - trailing byte count

        mov     [edi+3],al      ;U - write out first byte
        sub     esi,1           ;V - point to last src dword

        shr     ecx,2           ;U - shift down to dword count
        sub     edi,1           ;V - point to last dest dword

        cmp     ecx,8           ;U - test if small enough for unwind copy
        jb      short _memmove_CopyUnwindDown ;V - if so, then jump

        std                     ;N - set direction flag
        rep     movsd           ;N - move all of our dwords
        cld                     ;N - clear direction flag

        jmp     dword [_memmove_TrailDownVec + edx*4] ;N - process trailing bytes

        align   2

_memmove_LeadDown2:
        mov     al,[esi+3]      ;U - load first byte
        and     edx,ecx         ;V - trailing byte count

        mov     [edi+3],al      ;U - write out first byte
        mov     al,[esi+2]      ;V - get second byte from source

        shr     ecx,2           ;U - shift down to dword count
        mov     [edi+2],al      ;V - write second byte to destination

        sub     esi,2           ;U - point to last src dword
        sub     edi,2           ;V - point to last dest dword

        cmp     ecx,8           ;U - test if small enough for unwind copy
        jb      short _memmove_CopyUnwindDown ;V - if so, then jump

        std                     ;N - set direction flag
        rep     movsd           ;N - move all of our dwords
        cld                     ;N - clear direction flag

        jmp     dword [_memmove_TrailDownVec + edx*4] ;N - process trailing bytes

        align   2

_memmove_LeadDown3:
        mov     al,[esi+3]      ;U - load first byte
        and     edx,ecx         ;V - trailing byte count

        mov     [edi+3],al      ;U - write out first byte
        mov     al,[esi+2]      ;V - get second byte from source

        mov     [edi+2],al      ;U - write second byte to destination
        mov     al,[esi+1]      ;V - get third byte from source

        shr     ecx,2           ;U - shift down to dword count
        mov     [edi+1],al      ;V - write third byte to destination

        sub     esi,3           ;U - point to last src dword
        sub     edi,3           ;V - point to last dest dword

        cmp     ecx,8           ;U - test if small enough for unwind copy
        jb      _memmove_CopyUnwindDown  ;V - if so, then jump

        std                     ;N - set direction flag
        rep     movsd           ;N - move all of our dwords
        cld                     ;N - clear direction flag

        jmp     dword [_memmove_TrailDownVec + edx*4] ;N - process trailing bytes

;------------------------------------------------------------------

        align   2

_memmove_UnwindDown7:
        mov     eax,[esi+ecx*4+28] ;U - get dword from source
                                   ;V - spare
        mov     [edi+ecx*4+28],eax ;U - put dword into destination
        
_memmove_UnwindDown6:
        mov     eax,[esi+ecx*4+24] ;U(entry)/V(not) - get dword from source
                                   ;V(entry) - spare
        mov     [edi+ecx*4+24],eax ;U - put dword into destination
        
_memmove_UnwindDown5:
        mov     eax,[esi+ecx*4+20] ;U(entry)/V(not) - get dword from source
                                   ;V(entry) - spare
        mov     [edi+ecx*4+20],eax ;U - put dword into destination

_memmove_UnwindDown4:
        mov     eax,[esi+ecx*4+16] ;U(entry)/V(not) - get dword from source
                                   ;V(entry) - spare
        mov     [edi+ecx*4+16],eax ;U - put dword into destination

_memmove_UnwindDown3:
        mov     eax,[esi+ecx*4+12] ;U(entry)/V(not) - get dword from source
                                   ;V(entry) - spare
        mov     [edi+ecx*4+12],eax ;U - put dword into destination

_memmove_UnwindDown2:
        mov     eax,[esi+ecx*4+8] ;U(entry)/V(not) - get dword from source
                                   ;V(entry) - spare
        mov     [edi+ecx*4+8],eax ;U - put dword into destination

_memmove_UnwindDown1:
        mov     eax,[esi+ecx*4+4] ;U(entry)/V(not) - get dword from source
                                  ;V(entry) - spare
        mov     [edi+ecx*4+4],eax ;U - put dword into destination

        lea     eax,[ecx*4]     ;V - compute update for pointer

        add     esi,eax         ;U - update source pointer
        add     edi,eax         ;V - update destination pointer

_memmove_UnwindDown0:
        jmp     dword [_memmove_TrailDownVec + edx*4] ;N - process trailing bytes

		align	2

_memmove_UnwindDownVec:
		dd	_memmove_UnwindDown7
		dd	_memmove_UnwindDown6
		dd	_memmove_UnwindDown5
		dd	_memmove_UnwindDown4
		dd	_memmove_UnwindDown3
		dd	_memmove_UnwindDown2
		dd	_memmove_UnwindDown1
		dd	_memmove_UnwindDown0

;-----------------------------------------------------------------------------

        align   2

_memmove_TrailDown0:
        mov     eax,[ebp + 8]       ;U - return pointer to destination
                                ;V - spare
        pop     esi             ;U - restore esi
        pop     edi             ;V - restore edi
        leave
        ret

        align   2

_memmove_TrailDown1:
        mov     al,[esi+3]      ;U - get byte from source
                                ;V - spare
        mov     [edi+3],al      ;U - put byte in destination
        mov     eax,[ebp + 8]       ;V - return pointer to destination
        pop     esi             ;U - restore esi
        pop     edi             ;V - restore edi
        leave
        ret

        align   2

_memmove_TrailDown2:
        mov     al,[esi+3]      ;U - get first byte from source
                                ;V - spare
        mov     [edi+3],al      ;U - put first byte into destination
        mov     al,[esi+2]      ;V - get second byte from source
        mov     [edi+2],al      ;U - put second byte into destination
        mov     eax,[ebp + 8]       ;V - return pointer to destination
        pop     esi             ;U - restore esi
        pop     edi             ;V - restore edi
        leave
        ret

        align   2

_memmove_TrailDown3:
        mov     al,[esi+3]      ;U - get first byte from source
                                ;V - spare
        mov     [edi+3],al      ;U - put first byte into destination
        mov     al,[esi+2]      ;V - get second byte from source
        mov     [edi+2],al      ;U - put second byte into destination
        mov     al,[esi+1]      ;V - get third byte from source
        mov     [edi+1],al      ;U - put third byte into destination
        mov     eax,[ebp + 8]       ;V - return pointer to destination
        pop     esi             ;U - restore esi
        pop     edi             ;V - restore edi
        leave
        ret

		align 2
_memmove_TrailDownVec:
		dd	_memmove_TrailDown0
		dd	_memmove_TrailDown1
		dd	_memmove_TrailDown2
		dd	_memmove_TrailDown3

